﻿Imports System.ComponentModel
Imports DevExpress.Skins
Imports DevExpress.LookAndFeel
Imports DevExpress.UserSkins
Imports DevExpress.XtraBars
Imports DevExpress.XtraBars.Ribbon
Imports DevExpress.XtraBars.Helpers
Imports DevExpress.XtraSplashScreen
Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Grid

Public Class BH_BanHangNuocNgoai
    Dim Ma As String
    Dim State As Integer = 0 'trạng thái form '0 mặc định'1 tạo mới'2 sửa '3 xoa
    Dim DoanhThu, PhanTramThue, TienThue, TongCong As Decimal
    Dim DienGiaiThue, MaDonVi, TenDonVi, DiaChi As String
    Dim TaiKhoanNo, TaiKhoanCo, TaiKhoanThue As String
    Dim flagTyGia As Boolean
    Private flag As Boolean = False 'tránh loop xtragrid
    Private Sub setReadonly(ByVal b As Boolean)
        Me.MaTextEdit.Properties.ReadOnly = b
        Me.ManghiepvuSearchLookUpEdit.Properties.ReadOnly = b
        Me.SochungtuTextEdit.Properties.ReadOnly = b
        Me.NgaychungtuDateEdit.Properties.ReadOnly = b
        Me.SohoadonTextEdit.Properties.ReadOnly = b
        Me.NgaychungtuDateEdit.Properties.ReadOnly = b
        Me.SotokhaiHQTextEdit.Properties.ReadOnly = b
        Me.NgaytokhaiHQDateEdit.Properties.ReadOnly = b
        Me.TygiatokhaiHQTextEdit.Properties.ReadOnly = b
        Me.MakhachhangSearchLookUpEdit.Properties.ReadOnly = b
        Me.TenkhachhangTextEdit.Properties.ReadOnly = b
        Me.DiachiTextEdit.Properties.ReadOnly = b
        Me.HinhthucthanhtoanSearchLookUpEdit.Properties.ReadOnly = b
        Me.NgaythanhtoanDateEdit.Properties.ReadOnly = b
        Me.DieukiengiaohangTextEdit.Properties.ReadOnly = b
        Me.DiengiaiTextEdit.Properties.ReadOnly = b
        Me.PhuongtienvanchuyenTextEdit.Properties.ReadOnly = b
        Me.SoxeTextEdit.Properties.ReadOnly = b
        Me.NhanviensaleSearchLookUpEdit.Properties.ReadOnly = b
        Me.MachuongtrinhkhuyenmaiSearchLookUpEdit.Properties.ReadOnly = b
        Me.KyketoanTextEdit.Properties.ReadOnly = b
        Me.TinhtrangSearchLookUpEdit.Properties.ReadOnly = b
        Me.NgayhoadonDateEdit.Properties.ReadOnly = b
        Me.SoSOTextEdit.Properties.ReadOnly = b
        Me.SodongoiTextEdit.Properties.ReadOnly = b
        Me.SocontTextEdit.Properties.ReadOnly = b
        Me.SohopdongTextEdit.Properties.ReadOnly = b
        Me.BanggiaSearchLookUpEdit.Properties.ReadOnly = b
        Me.LoaitienSearchLookUpEdit.Properties.ReadOnly = b
        Me.NguyenteTextEdit.Properties.ReadOnly = b
        Me.ThanhtienvndTextEdit.Properties.ReadOnly = b
        Me.TygianganhangTextEdit.Properties.ReadOnly = b
        If b Then
            Me.IGridViewHangHoa.OptionsBehavior.Editable = False
            Me.IGridViewChatLuong.OptionsBehavior.Editable = False
            Me.IGridViewThue.OptionsBehavior.Editable = False
        Else
            Me.IGridViewHangHoa.OptionsBehavior.Editable = True
            Me.IGridViewChatLuong.OptionsBehavior.Editable = True
            Me.IGridViewHangHoa.OptionsBehavior.Editable = True
        End If
        If b Then 'trong trạng thái readonly  thì không cho nhập và dòng trống thêm ở cuối
            Me.IGridViewHangHoa.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
            Me.IGridViewThue.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
        Else  'cho thêm dòng trống ở cuối (khác form đơn - bỏ dòng : And State = 1)
            Me.IGridViewHangHoa.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom
            Me.IGridViewThue.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom
        End If
        If b Then
            'dxprovidervalidation
            Me.DxValidationProvider1.ValidationMode = DXErrorProvider.ValidationMode.Manual
            setButtonTrangThaiSua(False)
        Else
            Me.DxValidationProvider1.ValidationMode = DXErrorProvider.ValidationMode.Auto
            setButtonTrangThaiSua(True)
        End If
    End Sub
    Private Sub setButtonTrangThaiSua(ByVal b As Boolean) 'set nút lưu và xóa - khi ở trạng thái sửa thì mới cho phép thao tác trên nó (enable = true) - các trạng thái khác thì không cho (enable = false)
        Me.BarButtonItemLuu.Enabled = b
        Me.BarButtonItemXoa.Enabled = b
        If b Then 'xử lý nút sửa (nếu nút lưu - xóa cho phép thì sửa ko cho phép và ngược lại)
            Me.BarButtonItemSua.Enabled = False
        Else
            Me.BarButtonItemSua.Enabled = True
        End If
        setButtonChiTietTrangThaiSua(b)
    End Sub
    Private Sub setButtonChiTietTrangThaiSua(ByVal b As Boolean)
        Me.BarButtonItemTaoMoiChiTiet.Enabled = b
        Me.BarButtonItemXoaChiTiet.Enabled = b
        Me.BarButtonItemHuyThayDoiChiTiet.Enabled = b
        Me.BarButtonItemChuyenDuLieuThue.Enabled = b
    End Sub
    Private Sub loadDuLieu()
        SplashScreenManager.ShowForm(GetType(WaitForm1))
        Me.DMCheBienTableAdapter.Fill(Me.PCV_FISHDataSet.DMCheBien)
        Me.DMBaoQuanTableAdapter.Fill(Me.PCV_FISHDataSet.DMBaoQuan)
        Me.DMPhamCapTableAdapter.Fill(Me.PCV_FISHDataSet.DMPhamCap)
        Me.DMKichCoTableAdapter.Fill(Me.PCV_FISHDataSet.DMKichCo)
        Me.Dmhanghoa_donvitinhTableAdapter.Fill(Me.PCV_FISHDataSet.dmhanghoa_donvitinh)
        Me.DmhanghoaTableAdapter.FillByMaTenDonViTinh(Me.PCV_FISHDataSet.dmhanghoa)
        Me.DmkhachhangTableAdapter.FillByMaTenDiaChi(Me.PCV_FISHDataSet.dmkhachhang)
        'Me.BHBanHangTableAdapter.Fill(Me.PCV_FIScHDataSet.BHBanHang, 2)
        Me.BHThueTableAdapter.Fill(Me.PCV_FISHDataSet.BHThue)
        Me.BHBanHangChiTietTableAdapter.Fill(Me.PCV_FISHDataSet.BHBanHangChiTiet)
        Me.GetDMNhanVien_NhanVienBanHangTableAdapter.Fill(Me.PCV_FISHDataSet.getDMNhanVien_NhanVienBanHang)
        Me.GetPhanLoai_ThanhToanTableAdapter.Fill(Me.PCV_FISHDataSet.getPhanLoai_ThanhToan)
        Me.DmphongbanTableAdapter.Fill(Me.PCV_FISHDataSet.dmphongban)
        Me.GetPhanLoai_TienTeTableAdapter.Fill(Me.PCV_FISHDataSet.getPhanLoai_TienTe)
        Me.GetPhanLoai_TinhTrangPhieuTableAdapter.Fill(Me.PCV_FISHDataSet.getPhanLoai_TinhTrangPhieu)
        Me.DMTaiKhoanKeToanCap2TableAdapter.Fill(Me.PCV_FISHDataSet.DMTaiKhoanKeToanCap2)
        Me.GetDMTyGia_TyGiaTableAdapter.Fill(Me.PCV_FISHDataSet.getDMTyGia_TyGia, Today)
        Me.GetDMNghiepVu_BanHangNuocNgoaiTableAdapter.Fill(Me.PCV_FISHDataSet.getDMNghiepVu_BanHangNuocNgoai)
        Me.DmtygiaTableAdapter.Fill(Me.PCV_FISHDataSet.dmtygia)
        setReadonly(True)
        Me.BHBanHangIGridControl.ForceInitialize()
        Me.IGridViewDsBanHang.MoveLast()
        SplashScreenManager.CloseForm()
    End Sub

    Private Sub BH_BanHangNuocNgoai_Activated(sender As Object, e As System.EventArgs) Handles Me.Activated
        If Me.flagTyGia Then
            Me.DmtygiaTableAdapter.Fill(Me.PCV_FISHDataSet.dmtygia)
        End If
    End Sub
    Private Sub KH_HangHoa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.loadDuLieu()
    End Sub

    Private Sub BarButtonItemTaoMoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoi.ItemClick
        'focus
        Me.setReadonly(False)
        Me.ManghiepvuSearchLookUpEdit.Focus()
        Me.ManghiepvuSearchLookUpEdit.ShowPopup()
        'Mã //set ngầm
        Dim row As DataRowView = CType(BHBanHangBindingSource.AddNew(), DataRowView)
        Me.Ma = "BHNN" & System.Guid.NewGuid.ToString()
        row("ma") = Ma
        'mặc định
        Me.NgaychungtuDateEdit.EditValue = Today
        Me.NgayhoadonDateEdit.EditValue = Today
        Me.NgaythanhtoanDateEdit.EditValue = Today
        Me.NgaytokhaiHQDateEdit.EditValue = Today
        Me.TygianganhangTextEdit.EditValue = 1
        Me.LoaitienSearchLookUpEdit.EditValue = "VND"
        Me.ThanhtienvndTextEdit.EditValue = "0"
        Me.NguyenteTextEdit.EditValue = "0"
        Me.TinhtrangSearchLookUpEdit.EditValue = "NL"
        Me.State = 1
    End Sub

    Private Sub BarButtonItemLuu_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLuu.ItemClick
        Try
            Me.IGridViewHangHoa.PostEditor()
            Me.IGridViewHangHoa.UpdateCurrentRow()
            Me.IGridViewHangHoa.CloseEditor()
            Me.IGridViewChatLuong.PostEditor()
            Me.IGridViewChatLuong.UpdateCurrentRow()
            Me.IGridViewChatLuong.CloseEditor()
            Me.IGridViewThue.PostEditor()
            Me.IGridViewThue.UpdateCurrentRow()
            Me.IGridViewThue.CloseEditor()
            Me.BHBanHangBindingSource.EndEdit() 'end edit trước khi lưu - ghi những thay đổi 
            Me.FKBHBanHangChiTietBHBanHangBindingSource.EndEdit()
            Me.FKBHThueBHBanHangBindingSource.EndEdit()
            If Me.PCV_FISHDataSet.HasChanges Then 'coi có thay đổi dữ liệu hay ko ?
                If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn 'Lưu' không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                    Try
                        Me.TableAdapterManager.UpdateAll(Me.PCV_FISHDataSet)
                        Me.State = 0 'trở về trạng thái mặc định 0
                        Me.setReadonly(True) 'set lại readonly
                        setButtonTrangThaiSua(False) 'set lại nút 
                    Catch ex As Exception
                        DevExpress.XtraEditors.XtraMessageBox.Show("Xảy ra lỗi :" & ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try
                End If
            Else 'nếu ko thay đổi dữ liệu
                DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.setReadonly(True) 'set lại readonly
                setButtonTrangThaiSua(False) 'set lại nút 
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message) 'tung message khi có ngoại lệ
        End Try
    End Sub

    Private Sub NhanviensaleSearchLookUpEdit_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles NhanviensaleSearchLookUpEdit.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowhandle As Integer = Me.NhanviensaleSearchLookUpEdit.Properties.GetIndexByKeyValue(e.NewValue)
        Me.DmphongbanBindingSource.Filter = "ma = '" & editor.Properties.View.GetRowCellValue(rowhandle, "bophan") & "'"
    End Sub

    Private Sub BarButtonItemSua_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemSua.ItemClick
        Me.setReadonly(False)
        Me.State = 2
    End Sub

    Private Sub IGridView1_CellValueChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles IGridViewHangHoa.CellValueChanged
        Dim view1 As GridView = CType(sender, GridView)
        Dim dongianguyente, dongiavnd, thanhtiennguyente, thanhtienvnd, tienthueGTGT As Decimal
        If (e.Column.FieldName = "soluonggiao" OrElse e.Column.FieldName = "dongiavnd" OrElse e.Column.FieldName = "dongianguyente" OrElse e.Column.FieldName = "tilethueGTGT") AndAlso (Not flag) AndAlso IsDBNull(e.Value) = False Then
            Try
                dongianguyente = CDec(view1.GetRowCellValue(e.RowHandle, view1.Columns("dongianguyente")))
                dongiavnd = dongianguyente * CDec(Me.TygianganhangTextEdit.EditValue)
                thanhtiennguyente = CDec(Me.IGridViewHangHoa.GetFocusedRowCellValue("soluonggiao")) * dongianguyente
                thanhtienvnd = CDec(Me.IGridViewHangHoa.GetFocusedRowCellValue("soluonggiao")) * dongiavnd
                tienthueGTGT = thanhtienvnd * CDec(Me.IGridViewHangHoa.GetFocusedRowCellValue("tilethueGTGT")) / 100
            Catch ex As Exception
                Exit Sub
            End Try
            flag = True
            view1.SetRowCellValue(e.RowHandle, view1.Columns("dongiavnd"), dongiavnd)
            view1.SetRowCellValue(e.RowHandle, view1.Columns("thanhtiennguyente"), thanhtiennguyente)
            view1.SetRowCellValue(e.RowHandle, view1.Columns("thanhtienvnd"), thanhtienvnd)
            view1.SetRowCellValue(e.RowHandle, view1.Columns("tienthueGTGT"), tienthueGTGT)
            flag = False
        End If
        Me.NguyenteTextEdit.EditValue = CDec(Me.IGridViewHangHoa.Columns("thanhtiennguyente").SummaryItem.SummaryValue)
        Me.ThanhtienvndTextEdit.EditValue = CDec(Me.IGridViewHangHoa.Columns("thanhtienvnd").SummaryItem.SummaryValue)
        Me.IGridViewHangHoa.UpdateCurrentRow()
        Me.IGridViewHangHoa.UpdateSummary()
    End Sub

    Private Sub IGridViewHangHoa_FocusedRowChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles IGridViewHangHoa.FocusedRowChanged
        Dim currentView As GridView = CType(sender, GridView)
        Dim groupRowHandle As Integer
        If (e.FocusedRowHandle >= 0) Then
            groupRowHandle = currentView.GetParentRowHandle(e.FocusedRowHandle)
        Else
            groupRowHandle = e.FocusedRowHandle
        End If
        If Not currentView.IsGroupRow(groupRowHandle) Then Return
        Dim ht As Hashtable = currentView.GetGroupSummaryValues(groupRowHandle)
        Dim entry As DictionaryEntry
        For Each entry In ht
            Dim sumItem As GridGroupSummaryItem = entry.Key
            Dim val As Object = entry.Value
            Try
                If Me.IGridViewHangHoa.GetGroupRowValue(Me.IGridViewHangHoa.FocusedRowHandle) = "133" Then
                    Me.TienThue += CDec(val)
                    Me.DienGiaiThue = Me.IGridViewHangHoa.GetGroupRowValue(Me.IGridViewHangHoa.FocusedRowHandle, Me.IGridViewHangHoa.Columns("tenhang"))
                Else
                    Me.DoanhThu += CDec(val)
                End If
            Catch ex As Exception
            End Try
        Next
    End Sub
    Private Sub IGridView1_InitNewRow(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewHangHoa.InitNewRow
        'If State = 1 Then
        '    Me.IGridViewHangHoa.SetFocusedRowCellValue("ma", Ma)
        'End If
        Me.IGridViewHangHoa.SetFocusedRowCellValue("uid", System.Guid.NewGuid().ToString)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("manghiepvu", Me.ManghiepvuSearchLookUpEdit.EditValue)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("sochungtu", Me.SochungtuTextEdit.EditValue)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("ngaychungtu", CDate(Me.NgaychungtuDateEdit.EditValue))
        Me.IGridViewHangHoa.SetFocusedRowCellValue("soluongSO", 0) 'phân biệt chữ hoa thường
        Me.IGridViewHangHoa.SetFocusedRowCellValue("soluonggiao", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("dongianguyente", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("dongiavnd", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("thanhtiennguyente", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("thanhtienvnd", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("tilethueGTGT", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("tienthueGTGT", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("chiphiban", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("tilethueNK", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("tienthueNK", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("chietkhau", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("chenhlech", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("taikhoanno", TaiKhoanNo)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("taikhoanco", TaiKhoanCo)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("taikhoanthueGTGT", TaiKhoanThue)
    End Sub

    Private Sub BarButtonItemThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemThoat.ItemClick
        Me.Dispose() 'giải phóng ram
        Me.Close() 'đóng
    End Sub

    Private Sub BarButtonItemTaoMoiChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoiChiTiet.ItemClick
        Me.IGridViewHangHoa.MoveLast()
        Me.IGridViewHangHoa.Focus()
        Me.IGridViewHangHoa.AddNewRow()
        Me.IGridViewHangHoa.FocusedColumn = Me.IGridViewHangHoa.VisibleColumns(0)
        Me.IGridViewHangHoa.ShowEditor()
    End Sub

    Private Sub RepositoryItemSearchLookUpEdit5_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemSearchLookUpEditDMHangHoa.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        'If Me.IGridView1.IsNewItemRow(IGridView1.FocusedRowHandle) Then
        '    'And IGridView1.GetRow(IGridView1.FocusedRowHandle) Is DBNull.Value Then
        '    Me.IGridView1.AddNewRow()
        '    Me.IGridView1.MovePrev()
        'End If
        IGridViewHangHoa.SetFocusedRowCellValue("mahang", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        IGridViewHangHoa.SetFocusedRowCellValue("tenhang", editor.Properties.View.GetRowCellValue(rowHandle, "ten1"))
        IGridViewHangHoa.SetFocusedRowCellValue("dvt", editor.Properties.View.GetRowCellValue(rowHandle, "donvitinh"))
    End Sub
    Private Sub BarButtonItemFirst_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirst.ItemClick
        Me.BHBanHangBindingSource.MoveFirst()
    End Sub

    Private Sub BarButtonPrevious_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonPrevious.ItemClick
        Me.BHBanHangBindingSource.MovePrevious()
    End Sub

    Private Sub BarButtonNext_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonNext.ItemClick
        Me.BHBanHangBindingSource.MoveNext()
    End Sub

    Private Sub BarButtonLast_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonLast.ItemClick
        Me.BHBanHangBindingSource.MoveLast()
    End Sub

    Private Sub BarButtonItemFirstChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirstChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            Me.FKBHBanHangChiTietBHBanHangBindingSource.MoveFirst()
        End If
        If Me.LayoutControlGroupChatLuong.IsInTabbedGroup Then

        End If
    End Sub

    Private Sub BarButtonItemPreviousChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemPreviousChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            Me.FKBHBanHangChiTietBHBanHangBindingSource.MovePrevious()
        End If
        If Me.LayoutControlGroupChatLuong.IsInTabbedGroup Then

        End If
    End Sub

    Private Sub BarButtonItemNextChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemNextChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            Me.FKBHBanHangChiTietBHBanHangBindingSource.MoveNext()
        End If
        If Me.LayoutControlGroupChatLuong.IsInTabbedGroup Then

        End If
    End Sub

    Private Sub BarButtonItemLastChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLastChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            Me.FKBHBanHangChiTietBHBanHangBindingSource.MoveLast()
        End If
        If Me.LayoutControlGroupChatLuong.IsInTabbedGroup Then

        End If
    End Sub

    'Private Sub IGridViewHangHoa_InvalidRowException(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs) Handles IGridViewHangHoa.InvalidRowException
    '    e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction
    'End Sub

    'Private Sub IGridViewHangHoa_InvalidValueException(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.InvalidValueExceptionEventArgs) Handles IGridViewHangHoa.InvalidValueException
    '    e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction
    'End Sub
    '============= bắt lỗi igridview1 =========================
    Private Sub IGridView1_ShownEditor(ByVal sender As Object, ByVal e As System.EventArgs) Handles IGridViewHangHoa.ShownEditor
        Dim view As ColumnView = CType(sender, ColumnView)
        Try
            view.ActiveEditor.IsModified = True
        Catch ex As Exception
            Me.IGridViewHangHoa.SetFocusedRowCellValue("ma", Me.IGridViewDsBanHang.GetRowCellValue(Me.IGridViewDsBanHang.FocusedRowHandle, "ma"))
        End Try
        If TypeOf IGridViewHangHoa.ActiveEditor Is SearchLookUpEdit And IsDBNull(Me.IGridViewHangHoa.FocusedValue) Then
            DirectCast(IGridViewHangHoa.ActiveEditor, SearchLookUpEdit).ShowPopup()
        End If
    End Sub

    Private Sub IGridView1_ValidatingEditor(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs) Handles IGridViewHangHoa.ValidatingEditor
        If Me.IGridViewHangHoa.FocusedColumn.FieldName = "taikhoanno" And IGridViewHangHoa.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            End If
        ElseIf Me.IGridViewHangHoa.FocusedColumn.FieldName = "taikhoanco" And IGridViewHangHoa.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            End If
        ElseIf Me.IGridViewHangHoa.FocusedColumn.FieldName = "mahang" And IGridViewHangHoa.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            End If
        ElseIf Me.IGridViewHangHoa.FocusedColumn.FieldName = "solo" And IGridViewHangHoa.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            End If
        ElseIf Me.IGridViewHangHoa.FocusedColumn.FieldName = "hsd" And IGridViewHangHoa.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            End If
        ElseIf Me.IGridViewHangHoa.FocusedColumn.FieldName = "soluongSO" And IGridViewHangHoa.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            ElseIf CDec(e.Value) = 0 Then
                e.ErrorText = "không được bằng 0 !"
                e.Valid = False
                Exit Sub
            End If
        ElseIf Me.IGridViewHangHoa.FocusedColumn.FieldName = "soluonggiao" And IGridViewHangHoa.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            ElseIf CDec(e.Value) = 0 Then
                e.ErrorText = "không được bằng 0 !"
                e.Valid = False
                Exit Sub
            End If
        ElseIf Me.IGridViewHangHoa.FocusedColumn.FieldName = "tilethueGTGT" And IGridViewHangHoa.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            End If
        ElseIf Me.IGridViewHangHoa.FocusedColumn.FieldName = "tienthueGTGT" And IGridViewHangHoa.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            End If
        ElseIf Me.IGridViewHangHoa.FocusedColumn.FieldName = "taikhoanthueGTGT" And IGridViewHangHoa.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            End If
        ElseIf Me.IGridViewHangHoa.FocusedColumn.FieldName = "taikhoanchietkhau" And IGridViewHangHoa.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            End If
        ElseIf Me.IGridViewHangHoa.FocusedColumn.FieldName = "taikhoanthueNK" And IGridViewHangHoa.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            End If
        End If
    End Sub

    Private Sub BarButtonItemXoa_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoa.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa số chứng từ '" & Me.SochungtuTextEdit.EditValue & "' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
            Me.BHBanHangBindingSource.RemoveCurrent()
            Me.BHBanHangTableAdapter.Update(Me.PCV_FISHDataSet.BHBanHang)
            Me.setReadonly(True)
            Me.State = 0 'trở về mặc định
        End If
    End Sub

    Private Sub BarButtonItemHuyThayDoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoi.ItemClick
        Me.BHBanHangBindingSource.EndEdit()
        If Me.PCV_FISHDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi đang làm không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PCV_FISHDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub BarButtonItemXoaChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoaChiTiet.ItemClick
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 0 Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa '" & Me.IGridViewHangHoa.GetRowCellValue(Me.IGridViewHangHoa.FocusedRowHandle, "mahang") & "' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.IGridViewHangHoa.DeleteRow(Me.IGridViewHangHoa.FocusedRowHandle)
                Me.IGridViewHangHoa.UpdateCurrentRow()
                Me.IGridViewHangHoa.UpdateGroupSummary()
                Me.IGridViewHangHoa.UpdateSummary()
            End If
        End If
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 2 Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa '" & Me.IGridViewThue.GetRowCellValue(Me.IGridViewThue.FocusedRowHandle, "mahang") & "' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.IGridViewThue.DeleteRow(Me.IGridViewThue.FocusedRowHandle)
                Me.IGridViewThue.UpdateCurrentRow()
                Me.IGridViewThue.UpdateGroupSummary()
                Me.IGridViewThue.UpdateSummary()
            End If
        End If
    End Sub

    Private Sub BarButtonItemHuyThayDoiChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoiChiTiet.ItemClick
        Dim tabname As String
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 0 Then
            Me.FKBHBanHangChiTietBHBanHangBindingSource.EndEdit()
            tabname = "'Hàng hóa'"
        End If
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 1 Then
            Me.FKBHBanHangChiTietBHBanHangBindingSource.EndEdit()
            tabname = "'Chất lượng'"
        End If
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 2 Then
            Me.FKBHThueBHBanHangBindingSource.EndEdit()
            tabname = "'Thuế'"
        End If
        If Me.PCV_FISHDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi của phần " & tabname & " không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PCV_FISHDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub BarButtonItemLamTuoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLamTuoi.ItemClick
        Dim position As Integer = Me.BHBanHangBindingSource.Position 'load dữ liệu lại nhưng giữ nguyên phiếu
        loadDuLieu()
        Me.BHBanHangBindingSource.Position = position
    End Sub

    Private Sub MakhachhangSearchLookUpEdit_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles MakhachhangSearchLookUpEdit.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowhandle As Integer = Me.MakhachhangSearchLookUpEdit.Properties.GetIndexByKeyValue(e.NewValue)
        Me.TenkhachhangTextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "ten")
        Me.DiachiTextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "diachi")
    End Sub

    Private Sub TinhtrangSearchLookUpEdit_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TinhtrangSearchLookUpEdit.GotFocus
        If IsDBNull(Me.TinhtrangSearchLookUpEdit.EditValue) And State <> 0 And TinhtrangSearchLookUpEdit.IsPopupOpen = False Then
            SendKeys.Send("%{DOWN}")
        End If
    End Sub

    Private Sub TinhtrangSearchLookUpEdit_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TinhtrangSearchLookUpEdit.KeyDown
        If State <> 0 And (e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab) Then
            Me.TabbedControlGroup2.SelectedTabPage = Me.TabbedControlGroup2.TabPages(0) 'chọn tab hàng hóa
            Me.BHBanHangBindingSource.EndEdit()
            Me.IGridViewHangHoa.MoveLast()
            Me.IGridViewHangHoa.Focus()
            Me.IGridViewHangHoa.AddNewRow()
            Me.IGridViewHangHoa.FocusedColumn = Me.IGridViewHangHoa.VisibleColumns(0)
            Me.IGridViewHangHoa.ShowEditor()
        End If
    End Sub

    Private Sub BarButtonItem5_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemChuyenDuLieuThue.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn chuyển dữ liệu thuế tự động không ?'", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
            Me.IGridViewHangHoa.Columns("taikhoanno").GroupIndex = 0
            Me.IGridViewHangHoa.Columns("sohoadon").GroupIndex = 1
            For index = 0 To Me.IGridViewHangHoa.RowCount - 1
                Me.IGridViewHangHoa.MoveNext()
            Next
            Me.IGridViewHangHoa.ClearGrouping()
            Me.TabbedControlGroup2.SelectedTabPage = Me.TabbedControlGroup2.TabPages(2) 'chọn tab thue
            Me.IGridViewThue.AddNewRow()
        End If
    End Sub

    Private Sub IGridViewThue_InitNewRow(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewThue.InitNewRow
        'set mặc định
        'Me.IGridViewThue.SetFocusedRowCellValue("ma", Ma)
        Me.IGridViewThue.SetFocusedRowCellValue("uid", System.Guid.NewGuid().ToString)
        Me.IGridViewThue.SetFocusedRowCellValue("manghiepvu", Me.ManghiepvuSearchLookUpEdit.EditValue)
        Me.IGridViewThue.SetFocusedRowCellValue("sochungtu", Me.SochungtuTextEdit.EditValue)
        Me.IGridViewThue.SetFocusedRowCellValue("ngaychungtu", CDate(Me.NgaychungtuDateEdit.EditValue))
        '==================================================================
        Me.TongCong = CDec(Me.IGridViewHangHoa.Columns("thanhtienvnd").SummaryItem.SummaryValue)
        Try
            Me.PhanTramThue = Me.TienThue / Me.DoanhThu * 100
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Me.PhanTramThue = 0
        End Try
        Me.IGridViewThue.SetFocusedRowCellValue("diengiaithue", Me.DienGiaiThue)
        Dim position As Integer = Me.DmkhachhangBindingSource.Find("ma", Me.MakhachhangSearchLookUpEdit.EditValue)
        Me.IGridViewThue.SetFocusedRowCellValue("madonvi", Me.MakhachhangSearchLookUpEdit.EditValue)
        Me.IGridViewThue.SetFocusedRowCellValue("tendonvi", Me.MakhachhangSearchLookUpEdit.Text)
        Me.IGridViewThue.SetFocusedRowCellValue("diachi", Me.DiachiTextEdit.EditValue)
        Me.IGridViewThue.SetFocusedRowCellValue("masothue", Me.PCV_FISHDataSet.dmkhachhang.Rows(position).Item("masothue").ToString)
        Me.IGridViewThue.SetFocusedRowCellValue("doanhthu", Me.DoanhThu)
        Me.IGridViewThue.SetFocusedRowCellValue("phantramthue", Me.PhanTramThue) 'phân biệt chữ hoa thường
        Me.IGridViewThue.SetFocusedRowCellValue("tienthue", Me.TienThue)
        Me.IGridViewThue.SetFocusedRowCellValue("tongcong", Me.TongCong)
        Me.DoanhThu = 0
        Me.TienThue = 0
        Me.TongCong = 0
    End Sub

    Private Sub LoaitienSearchLookUpEdit_EditValueChanging(ByVal sender As System.Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles LoaitienSearchLookUpEdit.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowhandle As Integer = Me.LoaitienSearchLookUpEdit.Properties.GetIndexByKeyValue(e.NewValue)
        Me.TygianganhangTextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "tygia")
    End Sub

    Private Sub LoaitienSearchLookUpEdit_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoaitienSearchLookUpEdit.GotFocus
        If Me.State <> 0 Then
            'Me.GetDMTyGia_TyGiaTableAdapter.Fill(Me.PCV_FISHDataSet.getDMTyGia_TyGia, CDate(Me.NgaychungtuDateEdit.EditValue))
            Me.DmtygiaBindingSource.Filter = " ngaytygia = '" & Format(CDate(Me.NgaychungtuDateEdit.EditValue), "yyyy-MM-dd") & "'"
        End If
    End Sub

    Private Sub MakhachhangSearchLookUpEdit_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MakhachhangSearchLookUpEdit.GotFocus
        If IsDBNull(Me.MakhachhangSearchLookUpEdit.EditValue) And State <> 0 And MakhachhangSearchLookUpEdit.IsPopupOpen = False Then
            SendKeys.Send("%{DOWN}")
        End If
    End Sub

    Private Sub ManghiepvuSearchLookUpEdit_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles ManghiepvuSearchLookUpEdit.EditValueChanging
        'xử lý ngầm tài khoản
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowhandle As Integer = Me.ManghiepvuSearchLookUpEdit.Properties.GetIndexByKeyValue(e.NewValue)
        Me.TaiKhoanNo = editor.Properties.View.GetRowCellValue(rowhandle, "taikhoanno")
        Me.TaiKhoanCo = editor.Properties.View.GetRowCellValue(rowhandle, "taikhoanco")
        Me.TaiKhoanThue = editor.Properties.View.GetRowCellValue(rowhandle, "taikhoanvat")

    End Sub

    Private Sub ManghiepvuSearchLookUpEdit_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles ManghiepvuSearchLookUpEdit.GotFocus
        If IsDBNull(Me.ManghiepvuSearchLookUpEdit.EditValue) And State <> 0 And ManghiepvuSearchLookUpEdit.IsPopupOpen = False Then
            SendKeys.Send("%{DOWN}")
        End If
    End Sub

    Private Sub HinhthucthanhtoanSearchLookUpEdit_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles HinhthucthanhtoanSearchLookUpEdit.GotFocus
        If IsDBNull(Me.HinhthucthanhtoanSearchLookUpEdit.EditValue) And State <> 0 And HinhthucthanhtoanSearchLookUpEdit.IsPopupOpen = False Then
            SendKeys.Send("%{DOWN}")
        End If
    End Sub

    Private Sub NhanviensaleSearchLookUpEdit_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles NhanviensaleSearchLookUpEdit.GotFocus
        If IsDBNull(Me.NhanviensaleSearchLookUpEdit.EditValue) And State <> 0 And NhanviensaleSearchLookUpEdit.IsPopupOpen = False Then
            SendKeys.Send("%{DOWN}")
        End If
    End Sub

    Private Sub LoaitienSearchLookUpEdit_AddNewValue(ByVal sender As System.Object, ByVal e As DevExpress.XtraEditors.Controls.AddNewValueEventArgs) Handles LoaitienSearchLookUpEdit.AddNewValue
        DM_TyGia.Show()
        DM_TyGia.MdiParent = Me.MdiParent
        DM_TyGia.WindowState = FormWindowState.Maximized
        Me.flagTyGia = True
    End Sub

    Private Sub BarButtonItemIn_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemIn.ItemClick

    End Sub
End Class
